{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# xmesh: A 1k lines N-D Delaunay triangulation\n",
    "# xmesh-python: A 20 lines xtensor-numpy bindings for xmesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pymesh\n",
    "import bqplot.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2-D Mesh computed with xtensor, plotted with bqplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "points = np.random.randn(100, 2)\n",
    "mesh = pymesh.Mesh(points)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "cd5e03b46e674ba9951b820bad5c6149"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lines = np.stack(simplex.lines() for simplex in mesh)\n",
    "plt.figure()\n",
    "\n",
    "plt.plot(lines[:,:,0], \n",
    "         lines[:,:,1],\n",
    "         close_path=True,\n",
    "         colors=['red'],\n",
    "         stroke_width=1)\n",
    "\n",
    "plt.scatter(x=points[:,0],\n",
    "            y=points[:,1])\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A simple application with ipywidgets and bqplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ac8b3079e8504a76a248dfe6e0b7aca2"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "lines = np.stack(simplex.lines() for simplex in mesh)\n",
    "\n",
    "def update_lines(change):\n",
    "    with simplices.hold_sync():\n",
    "        points = np.stack([scatter.x, scatter.y], axis=1)\n",
    "        mesh = pymesh.Mesh(points)\n",
    "        lines = np.stack(simplex.lines() for simplex in mesh)\n",
    "        simplices.x = lines[:,:,0]\n",
    "        simplices.y = lines[:,:,1]\n",
    "\n",
    "plt.figure()\n",
    "simplices = plt.plot(lines[:,:,0], lines[:,:,1], close_path=True, colors=['red'], stroke_width=1)\n",
    "scatter = plt.scatter(x=points[:,0], y=points[:,1], enable_move=True)\n",
    "plt.show()\n",
    "\n",
    "scatter.observe(update_lines, names=['x', 'y'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
